import React, { Component } from 'react';
import PropTypes from 'prop-types';
import MaterialTable from 'material-table';
import Notifications from '../../../components/Notifications';
import ErrorPage from '../../../components/ErrorPage';
import DepartmentAPI from '../../../api/Departments';
import SubenclosuresAPI from '../../../api/Subenclosures';
const generateStateData = (subenclosures, locations) => {
let locationLookupIds = {};
locationLookupIds[0] = 'None';
locationLookupIds = locations.slice(0).reduce((acc, location) => {
acc[location.locationId] = location.location;
return acc;
}, locationLookupIds);
let locationLookup = {};
locationLookup.None = 'None';
locationLookup = locations.slice(0).sort((a, b) => (a.location > b.location ? 1 : -1)).reduce((acc, location) => {
acc[location.location] = location.location;
return acc;
}, locationLookup);
const subenclosuresData = subenclosures.map((sub) => ({
...sub,
locationId: sub.locationId || 0,
locationName: locationLookupIds[sub.locationId || 0],
}));
return {
subenclosuresData,
locationLookup,
};
};
class GroupDiets extends Component {
static async getInitialProps({ authToken }) {
const departmentsApi = new DepartmentAPI(authToken);
const subenclosuresApi = new SubenclosuresAPI(authToken);
try {
const [locRes, subenclosuresRes] = await Promise.all([
departmentsApi.getDepartments(),
subenclosuresApi.getSubenclosures(),
]);
return {
subenclosures: subenclosuresRes.data,
locations: locRes.data,
};
} catch (err) {
return {
subenclosures: [],
locations: [],
error: true,
errorMessage: 'Error loading data',
};
}
}
static propTypes = {